package pl.mw.cmd.internal.executor.format;

import pl.mw.cmd.executor.CommandExecutor;
import pl.mw.cmd.executor.ResultWriter;

/**
 * Implementacja {@link CommandExecutor} ktora formatuje dysk twardy.
 */
public class FormatExecutor implements CommandExecutor {
	
	/**
	 * 
	 */
	public FormatExecutor() {
		System.out.println("[pl.mw.cmd.executor.format] [FormatExecutor] Component created.");
	}
	
	/**
	 * Startuje parser.
	 */
	public void start() {
		System.out.println("[pl.mw.cmd.executor.format] [FormatExecutor] Component activated.");
	}

	/**
	 * Zatrzymuje parser.
	 */
	public void stop() {
		System.out.println("[pl.mw.cmd.executor.format] [FormatExecutor] Component deactivated.");
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see CommandExecutor#execute(String[], ResultWriter)
	 */
	@Override
	public void execute(String[] args, ResultWriter resultWriter) {
		
		if (args.length != 1) {
			resultWriter.write("Nieprawidlowa liczba argumentow.\r\n> ");
		} else {
			String disk = args[0].toUpperCase();
			resultWriter.write("Formatuje dysk " + disk + "\r\n");
			for (int i = 1; i <= 300; i++) {
				resultWriter.write(".");
				if (i % 50 == 0) {
					resultWriter.write("\r\n");
				}
				sleep(10);
			}
			resultWriter.write("Formatowanie zakonczone.\r\n> ");
		}
	}
	
	/**
	 * Zatrzymuje dzialanie na podana liczbe milisekund.
	 * @param miliseconds liczba milisekund
	 */
	public void sleep(long miliseconds) {
		try {
			Thread.sleep(miliseconds);
		} catch (InterruptedException e) {
			throw new IllegalStateException(e);
		}
	}
}
